package com.cz;

import lombok.SneakyThrows;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.TopicConfig;
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;

/*
 * @Description:
 * @Author: chenzhen
 * @Date: 2023/5/19 14:57
 */
public class CreateTopic {
    @SneakyThrows
    public static void main(String[] args) {
        String brokerAddress ="172.16.7.57:9876";
        DefaultMQAdminExt admin = new DefaultMQAdminExt();
        admin.setNamesrvAddr(brokerAddress); // 设置 NameServer 地址
        System.out.println(admin.getCreateTopicKey());
        try {
            admin.start();
            String topic = "TopicTest";
            int queueNum = 8; // 设置队列数
            TopicConfig topicConfig = new TopicConfig();
            topicConfig.setTopicName(topic);
            topicConfig.setReadQueueNums(queueNum);
            topicConfig.setWriteQueueNums(queueNum);
            // 创建主题
            admin.createAndUpdateTopicConfig(brokerAddress,topicConfig);
            System.out.println("Topic created successfully.");
        } catch (MQClientException e) {
            e.printStackTrace();
        } finally {
            admin.shutdown();
        }
    }
}
